﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Top.Web;
using Top.Data;
using Top.Core;
using Top.Web.Api;

namespace Top.PointStore.Business
{
    /*功能说明:积分明细操作管理类
    *版本:
    *参与人:王士锋
    *修改日期:2015.3.4
    */
    /// <summary>
    /// 积分明细操作类
    /// </summary>
    public static class PointDetailManager
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="pager"></param>
        /// <param name="pointKid"></param>
        /// <returns></returns>
        public static async Task<IList<T_PointDetailExpand>> GetList(Pager pager, int pointKid)
        {
            if (pointKid <= 0) return null;
            const string sql = "select a.*,b.F_PointAction FROM T_PointDetail_new a left join T_PointRule b on a.F_PointCode=b.F_PointCode where F_PointId = {0}";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, pointKid).ToList<T_PointDetailExpand>(pager.StartNumber, pager.EndNumber, "F_AddTime desc");
            }
        }

        /// <summary>根据用户编号获取数据列表
        /// </summary>
        public static async Task<IList<T_PointDetailExpand>> GetPageListByUserKid(Pager pager, OAuthUser user)
        {
            if (user ==null) return null;
            const string sql = @"select a.Kid,a.F_PointId,a.F_BuildingKid,a.F_PointCode,a.F_Point,a.F_AddTime,a.F_PointSource,b.F_PointAction FROM T_PointDetail_new a 
left join T_PointRule b on a.F_PointCode=b.F_PointCode where F_PointId in 
(select Kid from T_PointMain_new where F_UserToken={0} )";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, user.UserToken.ToString()).ToList<T_PointDetailExpand>(pager.StartNumber, pager.EndNumber, "a.F_AddTime desc");
            }
        }

        public static async Task<IList<T_PointDetailExpand>> GetPageListByPhone(Pager pager,string phone)
        {
            if (string.IsNullOrWhiteSpace(phone)) return null;
            const string sql = @"
            SELECT
	            m.*, n.F_PointAction
            FROM
	            t_pointdetail_new m
            LEFT JOIN t_pointrule n ON m.F_PointCode = n.F_PointCode
            WHERE
	            m.F_PointId = (
		            SELECT
			            c.Kid
		            FROM
			            t_broker a
		            LEFT JOIN t_oauthinfo b ON a.Kid = b.F_BrokerKid
		            LEFT JOIN t_pointmain_new c ON b.F_UserToken = c.F_UserToken
		            WHERE
			            a.F_Phone = {0}
		            LIMIT 1
	            )
            ";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, phone).ToList<T_PointDetailExpand>(pager.StartNumber, pager.EndNumber, " F_AddTime desc");
            }
        }

        public static async Task<IList<T_PointDetailExpand>> GetPageListByPointId(Pager pager, int pointId)
        {
            if (pointId<1) return null;
            const string sql = @"
            SELECT
	            m.*, n.F_PointAction
            FROM
	            t_pointdetail_new m
            LEFT JOIN t_pointrule n ON m.F_PointCode = n.F_PointCode
            WHERE
	            m.F_PointId = {0}
            ";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, pointId).ToList<T_PointDetailExpand>(pager.StartNumber, pager.EndNumber, " F_AddTime desc");
            }
        }

        internal static async Task<bool> AddPoint(T_PointMain_new pointMain, int point, string remark)
        {
            if (pointMain == null) return false;

            var pointDetail = new T_PointDetail()
            {
                F_AddTime = DateTime.Now,
                F_BuildingKid = 0,
                F_PointCode = remark,
                F_Point = point,
                F_PointSource = 4,
                F_PointId = pointMain.Kid
            };

            const string sql =
@"insert into T_PointDetail_new (F_PointId,F_BuildingKid,F_PointCode,F_Point,F_AddTime,F_PointSource)
                values({0:F_PointId},
                {0:F_BuildingKid},
                {0:F_PointCode},
                {0:F_Point},
                {0:F_AddTime},
                {0:F_PointSource});
                select LAST_INSERT_ID()";
            using (var db = new DbTour())
            {
                return await db.Sql(sql, pointDetail).ExecuteScalar<int>() > 0;
            }
        }

        public static async Task<bool> Add(T_PointDetail pointDetail)
        {
            if (pointDetail == null) return false;

            const string sql =
@"insert into T_PointDetail_new (F_PointId,F_BuildingKid,F_PointCode,F_Point,F_AddTime,F_PointSource)
                values({0:F_PointId},
                {0:F_BuildingKid},
                {0:F_PointCode},
                {0:F_Point},
                {0:F_AddTime},
                {0:F_PointSource});
                select LAST_INSERT_ID()";
            using (var db = DataBaseContext.GetDataBase("default"))
            {
                return await db.Sql(sql, pointDetail).ExecuteScalar<int>() > 0;
            }

        }

        internal static async Task<bool> AddPoint(T_PointMain_new pointMain, int point, string pointCode, int productExchangeKid, DbTour db)
        {
            var brokerPoint = await PointManager.Get(pointMain.F_UserToken);
            if (brokerPoint == null) return false;

            var pointDetail = new T_PointDetail()
            {
                F_AddTime = DateTime.Now,
                F_BuildingKid = 0,
                F_PointCode = pointCode,
                F_Point = point,
                F_PointSource = 4,
                F_PointId = brokerPoint.Kid,
                F_ProductExchangeKid = productExchangeKid
            };
            #region sql
            const string sql =
@"insert into T_PointDetail_new (F_PointId,F_BuildingKid,F_PointCode,F_Point,F_AddTime,F_PointSource,F_ProductExchangeKid)
                values({0:F_PointId},
                {0:F_BuildingKid},
                {0:F_PointCode},
                {0:F_Point},
                {0:F_AddTime},
                {0:F_PointSource},
                {0:F_ProductExchangeKid});
                select LAST_INSERT_ID()";

            return await db.Sql(sql, pointDetail).ExecuteScalar<int>() > 0;

            #endregion

        }
        /// <summary>
        /// 获取今日加积分
        /// </summary>
        /// <param name="userToken"></param>
        /// <returns></returns>
        public static async Task<int> GetAddTotalPointToday(string userToken)
        {
            var sql = @"SELECT IFNULL(SUM(d.F_Point),0) AS TotalAddPoint
                FROM t_pointdetail_new d
                JOIN t_pointmain_new m ON d.F_PointId=m.Kid
                WHERE d.F_AddTime>={0} AND d.F_AddTime<{1} AND d.F_Point>0 AND m.F_UserToken={2}";

            using (var db = DataBaseContext.GetDataBase("default"))
            {
                return await db.Sql(sql,DateTime.Now.Date,DateTime.Now.Date.AddDays(1),userToken).ExecuteScalar<int>();
            }
        }
    }
}
